/**
 * <p>Title: PlanDays.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2013-9-26 上午06:14:16  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.cInterface.cb;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zm.cb.domain.ZMCBPlanDays;

/**
 * 计划工作日
 * <p>Title: PlanDays.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2013-9-26 上午06:14:16  张志芳  Create
 *</pre>
 */
public class PlanDays {
	public static String NOT_WORKING = "不工作";
	public static double MAX_PERCENTAGE = 0.65;
	public static double MIN_PERCENTAGE = 0.3;
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	public static String getLongPlanEndDate(String beginDate,double monthNum){			
		String endDate = beginDate;				
		while(monthNum>0){
			String year = (Integer.parseInt(StaticTools.getYearStr(endDate))+1) + "";						
			int[] months = getMonthsForLong(query("1",year));
			for(int i=Integer.parseInt(StaticTools.getMonthStr(endDate));i<months.length;i++){
				if(monthNum<1){
					if(months[i]!=0){	
						endDate = StaticTools.getDate((int)(monthNum*StaticTools.getMonthDayNum(Integer.parseInt(year), i+1)), endDate, "yyyy-MM-dd");
						monthNum = 0;
						break;	
					}
					else{
						endDate = StaticTools.getMonthDate(1, endDate, "yyyy-MM-dd");
					}
				}
				else{
					endDate = StaticTools.getMonthDate(1, endDate, "yyyy-MM-dd");
					if(months[i]!=0){					
						monthNum--;
					}
				}
			}
			
		}			
		return endDate;
	}
	
	private static ZMCBPlanDays query(String fdType,String year){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fdType", fdType);
		if("1".equals(fdType)){
			map.put("fdItem", "全年工作月份");
		}
		else{
			map.put("fdItem", "全年每月实际工作天数");
		}		
		map.put("fdYear", year);
		List<ZMCBPlanDays> list = dao.query("ZMCBPlanDays.query", map);
		ZMCBPlanDays days = null;
		if(list.size()>0){
			days = list.get(0);
		}
		else{
			map.put("fdYear", "1");
			list = dao.query("ZMCBPlanDays.query", map);
			if(list.size()>0){
				days = list.get(0);
			}
		}
		return days;
	}
	
	public static int[] getMonthsForLong(ZMCBPlanDays planDays){
		int[] days = {1,1,1,1,1,1,1,1,1,1,1,1};
		if(planDays==null)
			return days;
		if(planDays.getFdJan().equals(NOT_WORKING))
			days[0]=0;
		else if(planDays.getFdFeb().equals(NOT_WORKING))
			days[1]=0;
		else if(planDays.getFdMar().equals(NOT_WORKING))
			days[2]=0;
		else if(planDays.getFdApr().equals(NOT_WORKING))
			days[3]=0;
		else if(planDays.getFdMay().equals(NOT_WORKING))
			days[4]=0;
		else if(planDays.getFdJun().equals(NOT_WORKING))
			days[5]=0;
		else if(planDays.getFdJul().equals(NOT_WORKING))
			days[6]=0;
		else if(planDays.getFdAug().equals(NOT_WORKING))
			days[7]=0;
		else if(planDays.getFdSep().equals(NOT_WORKING))
			days[8]=0;
		else if(planDays.getFdOct().equals(NOT_WORKING))
			days[9]=0;
		else if(planDays.getFdNov().equals(NOT_WORKING))
			days[10]=0;
		else if(planDays.getFdDec().equals(NOT_WORKING))
			days[11]=0;		
		return days;
	}
	
	public static double[] getMonthsForYear(ZMCBPlanDays planDays){
		double[] days = {1,1,1,1,1,1,1,1,1,1,1,1};
		if(planDays==null)
			return days;
		double total = Double.parseDouble(planDays.getFdJan())+Double.parseDouble(planDays.getFdFeb())+
						Double.parseDouble(planDays.getFdMar())+Double.parseDouble(planDays.getFdApr())+
						Double.parseDouble(planDays.getFdMay())+Double.parseDouble(planDays.getFdJun())+
						Double.parseDouble(planDays.getFdJul())+Double.parseDouble(planDays.getFdAug())+
						Double.parseDouble(planDays.getFdSep())+Double.parseDouble(planDays.getFdOct())+
						Double.parseDouble(planDays.getFdNov())+Double.parseDouble(planDays.getFdDec());
		
		days[0]=getMonthPercent(total,Double.parseDouble(planDays.getFdJan()));
		days[1]=getMonthPercent(total,Double.parseDouble(planDays.getFdFeb()));
		days[2]=getMonthPercent(total,Double.parseDouble(planDays.getFdMar()));
		days[3]=getMonthPercent(total,Double.parseDouble(planDays.getFdApr()));
		days[4]=getMonthPercent(total,Double.parseDouble(planDays.getFdMay()));
		days[5]=getMonthPercent(total,Double.parseDouble(planDays.getFdJun()));
		days[6]=getMonthPercent(total,Double.parseDouble(planDays.getFdJan()));
		days[7]=getMonthPercent(total,Double.parseDouble(planDays.getFdJul()));
		days[8]=getMonthPercent(total,Double.parseDouble(planDays.getFdAug()));
		days[9]=getMonthPercent(total,Double.parseDouble(planDays.getFdSep()));
		days[10]=getMonthPercent(total,Double.parseDouble(planDays.getFdOct()));
		days[11]=getMonthPercent(total,Double.parseDouble(planDays.getFdNov()));
		days[12]=getMonthPercent(total,Double.parseDouble(planDays.getFdDec()));
	
		return days;
	}		
	
	public static String getYearPlanEndDate(String beginDate,double monthNum){
		String endDate = beginDate;				
		while(monthNum>=1){
			String year = (Integer.parseInt(StaticTools.getYearStr(endDate))+1) + "";						
			double[] months = getMonthsForYear(query("1",year));
			for(int i=Integer.parseInt(StaticTools.getMonthStr(endDate));i<months.length;i++){
				if(monthNum<1){
					if(months[i]>0){
						if(monthNum < months[i]) {
							endDate = StaticTools.getDate((int)(monthNum*StaticTools.getMonthDayNum(Integer.parseInt(year), i+1)), endDate, "yyyy-MM-dd");
							monthNum = 0;
							break;	
						}
						else{
							endDate = StaticTools.getMonthDate(1, endDate, "yyyy-MM-dd");
							monthNum = monthNum - months[i];
						}
					}
					else{
						endDate = StaticTools.getMonthDate(1, endDate, "yyyy-MM-dd");
					}
				}
				else{
					endDate = StaticTools.getMonthDate(1, endDate, "yyyy-MM-dd");
					if(months[i]!=0){					
						monthNum = monthNum - months[i];
					}
				}				
			}
		}			
		endDate = StaticTools.getDate((int)(monthNum*30), endDate, "yyyy-MM-dd");
		return endDate;
	}
	
	private static double getMonthPercent(double total,double days){
		double r = days/total;
		if(r>MAX_PERCENTAGE){
			return 1;
		}
		else if(r<MAX_PERCENTAGE && r>MIN_PERCENTAGE){
			return 0.5;
		}
		else{
			return 0;
		}
	}
}
